<template>
  <div class="row">
    <!-- 一级分类-->
    <div class="col-xs-6">
      <p>
        <button @click="addLevel1">
          <i class="ace-icon fa fa-edit"></i>
          新增一级分类
        </button>
        &nbsp;
        <button @click="refresh">
          <i class="ace-icon fa fa-undo"></i>
          刷新
        </button>
      </p>
      <table id="level1-simple-table" class="table  table-bordered table-hover">
        <thead>
        <tr>
          <th>id</th>
          <th>名称</th>
          <th>顺序</th>
          <th>操作</th>
        </tr>
        </thead>

        <tbody>
        <tr v-for="category in level1" :key="category.id" @click="onClickLevel1(category)" :class="{ 'active' : category.id
         === active.id }">
          <td>{{ category.id }}</td>
          <td>{{ category.name }}</td>
          <td>{{ category.sort }}</td>
          <td>
            <div class="hidden-sm hidden-xs btn-group">

              <button @click="edit(category)" class="btn btn-xs btn-info">
                <i class="ace-icon fa fa-pencil bigger-120"></i>
              </button>

              <button @click="del(category.id)" class="btn btn-xs btn-danger">
                <i class="ace-icon fa fa-trash-o bigger-120"></i>
              </button>
            </div>

            <div class="hidden-md hidden-lg">
              <div class="inline pos-rel">
                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                  <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                </button>

                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                  <li>
                    <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
    <!-- 二级分类 -->
    <div class="col-md-6">
      <p>
        <button @click="addLevel2">
          <i class="ace-icon fa fa-edit"></i>
          新增二级分类
        </button>
      </p>
      <table id="level2-simple-table" class="table  table-bordered table-hover">
        <thead>
        <tr>
          <th>id</th>
          <th>名称</th>
          <th>顺序</th>
          <th>操作</th>
        </tr>
        </thead>

        <tbody>
        <tr v-for="category in level2" :key="category.id">
          <td>{{ category.id }}</td>
          <td>{{ category.name }}</td>
          <td>{{ category.sort }}</td>
          <td>
            <div class="hidden-sm hidden-xs btn-group">

              <button @click="edit(category)" class="btn btn-xs btn-info">
                <i class="ace-icon fa fa-pencil bigger-120"></i>
              </button>

              <button @click="del(category.id)" class="btn btn-xs btn-danger">
                <i class="ace-icon fa fa-trash-o bigger-120"></i>
              </button>
            </div>

            <div class="hidden-md hidden-lg">
              <div class="inline pos-rel">
                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                  <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                </button>

                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                  <li>
                    <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Modal -->
  <div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">表单</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal">
                 <div class="form-group">
                   <label class="col-sm-2 control-label">父分类</label>
                   <div class="col-sm-10">
                   <p class="form-control">{{ active.name || '无' }}</p>
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">名称</label>
                   <div class="col-sm-10">
                   <input v-model="category.name" type="text" class="form-control">
                </div>
              </div>
                 <div class="form-group">
                   <label class="col-sm-2 control-label">顺序</label>
                   <div class="col-sm-10">
                   <input v-model="category.sort" type="text" class="form-control">
                </div>
              </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
          <button @click="saveCategory" type="button" class="btn btn-primary">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import {onMounted, ref} from "vue";
import pagination from "@/components/pagination";

export default {
  name: "Category",
  components: {
    pagination,
  },
  setup() {
    // categorys 数组
    const categorys = ref([]);
    const category = ref({}); // 保存分类表单

    // 一级分类
    const level1 = ref([]);
    // 二级分类
    const level2 = ref([]);
    // 点击激活的一级分类
    const active = ref(0);
    // 枚举字段

    // 刷新
    const refresh = () => {
      getCategoryList();
    }

    // 获取分类列表
    const getCategoryList = () => {
      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/business/admin/category/all").then((response) => {
        Loading.hide();
        const resp = response.data;
        categorys.value = resp.content;
        // 分类数据 处理成 树形结构
        level1.value = [];
        for (let i = 0; i < categorys.value.length; i++) {
          let c = categorys.value[i];
          // 如果是一级分类
          if (c.parent === '00000000') {
            level1.value.push(c);
            // 看看一级分类下还有没有二级分类
            for (let j = 0; j < categorys.value.length; j++) {
              let child = categorys.value[j];
              if (child.parent === c.id) {
                if (Tool.isEmpty(c.children)) {
                  c.children = [];
                }
                c.children.push(child);
              }
            }
          }
        }

        level2.value = [];
        setTimeout(() => {
          $("tr.active").trigger("click");
        }, 100);
      })
    }

    // 保存分类
    const saveCategory = () => {
      // 保存参数校验
      if ( 1 != 1
                || !Validator.require(category.value.parent, "父id")
                || !Validator.require(category.value.name, "名称")
                || !Validator.length(category.value.name, "名称", 1, 50)
        )
      {
        return;
      }

      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/business/admin/category/save", category.value).then((response) => {
        const resp = response.data;
        if (resp.success) {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.success("保存成功");
          getCategoryList();
        } else {
          Loading.hide();
          $('#form-modal').modal('hide');
          Toast.warning(resp.message);
        }
      })
    }

    // 删除分类
    const del = (id) => {
      Confirm.show("删除后不可恢复，确认删除？", function () {
        Loading.show();
        axios.delete(process.env.VUE_APP_SERVER + "/business/admin/category/delete/" + id).then((response) => {
          const resp = response.data;
          if (resp.success) {
            //$('#form-modal').modal('hide');
            getCategoryList();
          }
        })
        Toast.success("删除成功");
      })
    }

    // 添加 一级分类
    const addLevel1 = () => {
      active.value = {};
      category.value = {
        parent: '00000000'
      };
      $('#form-modal').modal('show');
    }

    // 添加 二级分类
    const addLevel2 = () => {
      if (Tool.isEmpty(active.value)) {
        Toast.warning("请先点击一级分类");
        return;
      }
      category.value = {
        parent: active.value.id
      };
      $('#form-modal').modal('show');
    }

    // 修改分类
    const edit = (param) => {
      // 方法一
      //category.value = JSON.parse(JSON.stringify(param));
      // 方法二
      category.value = $.extend({}, param);
      $('#form-modal').modal('show');
    }

    /**
     * 点击一级分类 表格
     */
    const onClickLevel1 = (level1Data) => {
      active.value = level1Data;
      // 渲染 二级分类
      level2.value = level1Data.children;
    }
    // 初始加载第一页
    onMounted(() => {
      getCategoryList();
    })

    return {
      categorys,
      refresh,
      getCategoryList,
      addLevel1,
      addLevel2,
      saveCategory,
      category,
      edit,
      del,
      level1,
      level2,
      onClickLevel1,
      active,
    }
  }
}
</script>

<style scoped>
.active td {
  background-color: rgba(165, 231, 200, 0.95) !important;
}
</style>